<?php
require_once 'GlobalSettings.php';
require_once 'resource/UploadManager.php';
require_once 'resource/FileResourceManager.php';
require_once 'user/UserManager.php';
require_once 'user/AccessControlManager.php';
require_once 'LerniBasicController.php';
require_once 'utility/KeywordSearch.php';
require_once 'utility/Utility.php';

class FileResourceController extends LerniBasicController
{		    
	 const PN_POS = 'SEARCH_POS';	
	 const PN_PRE_POS = 'PRE_SEARCH_POS';
	 
	 const PN_KEYWORKDS = 'KEYWORDS';


	 const POS_SEP = 'A';
	 
	 protected $keywordsStr = null;
	 protected $prePosArray = array();
	 protected $currentPos = array(0,0);
	 
	public function init()
	{
	   parent::init();
	   $this->view->seachResult = array();
	}
	 
     public function searchAction()
	 {			   
	    /////////////parse  and check count of workds.
		$this->keywordsStr = $this->getRequest()->getParam(self::PN_KEYWORKDS);
		
		 if(!$this->getRequest()->isPost())
		 {
		    $this->keywordsStr = base64_decode($this->keywordsStr);			
		 }
		 $this->view->keywordsStr = $this->keywordsStr;
		 
		 if(strlen($this->keywordsStr) > GlobalSettings::MAX_INPUT_STR_LEN)
		 {
		    $this->renderError(
			"Only " . GlobalSettings::MAX_INPUT_STR_LEN . " characters are allowed!"
			);
			return;
		 }		 
		 $keywords = Utility::stringToWordsArray($this->keywordsStr);
		 if(count($keywords) > GlobalSettings::MAX_KEYWORDS_COUNT)
		 {
		    $this->renderError(
			"No more than " . GlobalSettings::MAX_KEYWORDS_COUNT . " words are allowed!"
			);
		   return;
		 }
		 
		 
		 //////////////////////////debug code /////////////////////////////
		 $this->view->message = " Keywords Count:".count($keywords) . "<br/>";
		 foreach($keywords as $val)
		 {
		    $this->view->message .= $val . "<br/>";
		 }
		 $this->view->message .= " <br/>=============<br/>";
		 /////////////////////////////////////////////////////////
		 
		 ///create previous page url
		$this->createPreviousPageUrl();
		 
		 
		///search depends on PN_POS
		$posStr = $this->getRequest()->getParam(self::PN_POS);		
		$searchState = new SearchState();		
		if(isset($posStr))
		{
		   $this->currentPos = explode(self::POS_SEP,$posStr);
		   $searchState->searchMode = $this->currentPos[0];
		   $searchState->posInSearchMode = $this->currentPos[1];
		}
								 
		$this->view->seachResult = FileResourceManager::instance()->search($keywords,$searchState);
						
		////////////create next page url
		$this->createNextPageUrl($searchState);
		
	     $this->render("index");
	 }
	 
	 
	 /*
	 *It must be called after createPreviousPageUrl being called.
	 */
	 protected function createNextPageUrl($searchState)
	 {
	    if($searchState->completed)
		{
		   $this->view->nextSearchUrl = null;
		   return;
		}
		
		$this->view->nextSearchUrl = $this->getRequest()->getPathInfo() . '?';			
		$this->view->nextSearchUrl .= self::PN_POS . "=$searchState->searchMode";
		$this->view->nextSearchUrl .= self::POS_SEP . "$searchState->posInSearchMode";
			
		$this->view->nextSearchUrl .= 			
		'&&' . self::PN_KEYWORKDS . '=' . base64_encode($this->keywordsStr);	
			
		$this->view->nextSearchUrl .= '&&' . self::PN_PRE_POS . '=';
		
		$prePosSize = count($this->prePosArray);
		if( $prePosSize > 1
		&& $this->prePosArray[$prePosSize - 2] == $this->currentPos[0] )
		{
		   $this->prePosArray[$prePosSize - 1] = $this->currentPos[1];		  
		}
		else
		{
		    $this->prePosArray = array_merge($this->prePosArray,$this->currentPos);
			$prePosSize += 2;
		}
		
		$this->view->nextSearchUrl .= $this->prePosArray[0];
		for($i = 1;$i < $prePosSize;$i++)
		{
			$this->view->nextSearchUrl .= self::POS_SEP . $this->prePosArray[$i];
		}	
		 
	 }
	 
	 protected function createPreviousPageUrl()
	 {	 
		$this->view->previousSearchUrl = null;
		$prePosStr = $this->getRequest()->getParam(self::PN_PRE_POS);		
		if(!isset($prePosStr) || strlen($prePosStr) < 3)
		{
		    return null;
		}
				
		$this->prePosArray = explode(self::POS_SEP,$prePosStr);
		$prePosSize = count($this->prePosArray);
		
		if($prePosSize < 2 )
		{
		   return;
		}
		$lastMode = $this->prePosArray[$prePosSize - 2];
		$lastPos = $this->prePosArray[$prePosSize - 1];
		
		$this->view->previousSearchUrl = $this->getRequest()->getPathInfo() . '?';		
		$this->view->previousSearchUrl .= self::PN_POS . '=' . $lastMode . self::POS_SEP . $lastPos;
		$this->view->previousSearchUrl .= '&&';
		$this->view->previousSearchUrl .= self::PN_KEYWORKDS . '=' . base64_encode($this->keywordsStr);		
		
		if($lastPos >= GlobalSettings::SEARCH_PAGE_SIZE)
		{
		    $lastPos -= GlobalSettings::SEARCH_PAGE_SIZE;
			$this->prePosArray[$prePosSize - 1] = $lastPos;			
		}
		else
		{		  
		   $prePosSize -= 2;
		}
		
		
		if($prePosSize > 1)
		{
		    $this->view->previousSearchUrl .= '&&' . self::PN_PRE_POS . "=" . $this->prePosArray[0];
			for($i = 1;$i < $prePosSize;$i++)
			{
			    $this->view->previousSearchUrl .= self::POS_SEP . $this->prePosArray[$i];
			}
		}
	 }
};



?>